﻿namespace Fiddler
{
    using System;
    using System.Diagnostics;
    using System.Windows.Forms;

    public abstract class Inspector2
    {
        protected Inspector2()
        {
        }

        public abstract void AddToTab(TabPage o);
        public virtual void AssignSession(Session oS)
        {
            if (this is IRequestInspector2)
            {
                IRequestInspector2 inspector = (IRequestInspector2) this;
                inspector.headers = oS.oRequest.headers;
                inspector.body = oS.requestBodyBytes;
                inspector.bReadOnly = (oS.state != SessionStates.HandTamperRequest) && !oS.oFlags.ContainsKey("x-Unlocked");
            }
            else if (this is IResponseInspector2)
            {
                IResponseInspector2 inspector2 = (IResponseInspector2) this;
                inspector2.headers = oS.oResponse.headers;
                inspector2.body = oS.responseBodyBytes;
                inspector2.bReadOnly = (oS.state != SessionStates.HandTamperResponse) && !oS.oFlags.ContainsKey("x-Unlocked");
            }
        }

        public virtual bool CommitAnyChanges(Session oS)
        {
            bool flag = false;
            if (this is IRequestInspector2)
            {
                IRequestInspector2 inspector = (IRequestInspector2) this;
                if (!inspector.bDirty)
                {
                    return false;
                }
                if (((oS.state != SessionStates.HandTamperRequest) && !oS.oFlags.ContainsKey("x-Unlocked")) && (((inspector as Inspector2).GetFlags() & InspectorFlags.AlwaysCommitEdits) == InspectorFlags.None))
                {
                    return false;
                }
                if (inspector.headers != null)
                {
                    flag = true;
                    oS.oRequest.headers = inspector.headers;
                }
                if (inspector.body != null)
                {
                    flag = true;
                    oS.requestBodyBytes = inspector.body;
                    if ((oS.oRequest.headers != null) && oS.oRequest.headers.Exists("Content-Length"))
                    {
                        oS.oRequest.headers["Content-Length"] = oS.requestBodyBytes.LongLength.ToString();
                    }
                }
                this.UnsetDirtyFlag();
                return flag;
            }
            if (!(this is IResponseInspector2))
            {
                return false;
            }
            IResponseInspector2 inspector2 = (IResponseInspector2) this;
            if (!inspector2.bDirty)
            {
                return false;
            }
            if (((oS.state != SessionStates.HandTamperResponse) && !oS.oFlags.ContainsKey("x-Unlocked")) && (((inspector2 as Inspector2).GetFlags() & InspectorFlags.AlwaysCommitEdits) == InspectorFlags.None))
            {
                return false;
            }
            if (inspector2.headers != null)
            {
                flag = true;
                oS.oResponse.headers = inspector2.headers;
            }
            if (inspector2.body != null)
            {
                flag = true;
                oS.responseBodyBytes = inspector2.body;
                if ((oS.oResponse.headers != null) && oS.oResponse.headers.Exists("Content-Length"))
                {
                    oS.oResponse.headers["Content-Length"] = oS.responseBodyBytes.LongLength.ToString();
                }
            }
            this.UnsetDirtyFlag();
            return flag;
        }

        public virtual InspectorFlags GetFlags()
        {
            return InspectorFlags.None;
        }

        public abstract int GetOrder();
        public virtual int ScoreForContentType(string sMIMEType)
        {
            return 0;
        }

        public virtual int ScoreForSession(Session oS)
        {
            string sString = string.Empty;
            if (this is IRequestInspector2)
            {
                if (oS.oRequest != null)
                {
                    sString = oS.oRequest["Content-Type"];
                }
            }
            else if (oS.oResponse != null)
            {
                sString = oS.oResponse["Content-Type"];
            }
            sString = Utilities.TrimAfter(sString, ';');
            return this.ScoreForContentType(sString);
        }

        public virtual void SetFontSize(float flSizeInPoints)
        {
        }

        public virtual void ShowAboutBox()
        {
            FiddlerApplication.DoNotifyUser(this.ToString() + "\n\n" + FileVersionInfo.GetVersionInfo(base.GetType().Assembly.Location).ToString(), "About Inspector", MessageBoxIcon.Asterisk);
        }

        public virtual bool UnsetDirtyFlag()
        {
            return false;
        }
    }
}

